<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Questions extends Model
{
    public function saveQuestions($questionsId,$questionsName,$questionsArr)
    {
        $questions = new Questions;
        if ($questionsId == '0'){
            $questions->question_name = $questionsName;
            $questions->questions = $questionsArr;
            $questions->created = time();
            $res = $questions->save();
            if ($res == false){
                return $res;
            }else {
                return $questions->id;
            }
        }else {
            $list = [
                'question_name' => $questionsName,
                'questions' => $questionsArr
            ];
            $res = $questions->save($list,['id' =>$questionsId]);
            return $res;
        }
    }
    
    public function getWarehouseAvgScore($questions_id)
    {
        $sql = 'SELECT warehouse.id warehouse_id,warehouse.warehouse warehouse_name,avg(log.score) avg_score';
        $sql .=' FROM dsl_warehouse warehouse LEFT JOIN dsl_questionslog log';
        $sql .=' ON log.warehouse_id = warehouse.id';
        $sql .=' GROUP BY warehouse_id,warehouse_name';
        $warehouseAvgScore = Db::query($sql);
        if ($warehouseAvgScore != false || $warehouseAvgScore != null){
            foreach ($warehouseAvgScore as $wk => &$wv){
                if ($wv['avg_score'] == null){
                    $wv['avg_score'] = '0';
                }
            }
        }
        return $warehouseAvgScore;
    }
    
    public function getFalseNum($questions_id,$warehouse_id = 'all')
    {
        if ($warehouse_id == 'all'){
            $questionslog_list = db('questionslog')->where('questions_id',$questions_id)->column('questions');
        }else{
            $questionslog_list = db('questionslog')->where(['questions_id'=>$questions_id,'warehouse_id'=>$warehouse_id])->column('questions');
        }
        $false_num = array();
        $questions = db('questions')->where('id',$questions_id)->value('questions');
        $questions = json_decode($questions,true);
        foreach ($questions as $qk => $qv){
            $sub_fail_arr = array();
            $sub_fail_arr['question'] = $qv['question'];
            $sub_fail_arr['false_num'] = 0;
            foreach ($questionslog_list as $logk => $logv){
               $logv = json_decode($logv,true);
                foreach ($logv as $logvk => &$logvv){
                    if ($logvv['question_id'] == $qv['question_id']){
                        if ($logvv['iftrue'] == false || $logvv['iftrue'] == 'false'){
                            $sub_fail_arr['false_num'] ++;
                            continue;
                    
                        }
                    }
                }
             
            }
            array_push($false_num, $sub_fail_arr);
        }
        return $false_num;
        
    }
    
    public function getWarehouseFalseNum($questions_id)
    {
        $warehouse_list = db('warehouse')->field(['id','warehouse'])->select();
        if ($warehouse_list == false){
            return null;
        }
        foreach ($warehouse_list as $wk => &$wv){
            $wv['questions'] = $this->getFalseNum($questions_id, $wv['id']);
        }
        return $warehouse_list;
    }
}